<?php
/**
 *	Views of Reports.
 *	@package		tools.BugTracker
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		1.0
 */
/**
 *	Views of Reports.
 *	@package		tools.BugTracker
 *	@extends		Framework_Neon_DefinitionView
 *	@uses			BugStatements
 *	@uses			Logic_Bug
 *	@uses			Logic_User
 *	@uses			Logic_Project
 *	@uses			Logic_Server
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		1.0
 *	@todo			Language Support of Messages
 *	@todo			Code Documentation
 */
class View_Bug_Report extends CMF_Krypton_Core_DefinitionView
{
	public function __construct()
	{
		parent::__construct( TRUE );
	}
	/**
	 *	Builds Content Views.
	 *	@access		public
	 *	@return		void
	 */
	public function buildContent()
	{
		$request	= $this->registry->get( 'request' );
		$auth		= $this->registry->get( 'auth' );

		if( $request->get( 'bug_id' ) )
		{
			if( $auth->hasRight( "report", "view" ) )
				$content = $this->buildContentView();
		}
		else
			$content	= $this->buildContentAdd();
		return $content;
	}
	
	/**
	 *	Builds Control View from Wiki File.
	 *	@access		public
	 *	@return		string
	 */
	public function buildExtra()
	{
		return $this->loadContent( "report_control.wiki" );
	}
	
	/**
	 *	...
	 *	@access		public
	 *	@param		...
	 *	@param		...
	 *	@return		string
	 */
	public function getIconURL( $priority, $status )
	{
		$config	= $this->registry->get( 'config' );
		$url	= $config['paths']['themes'].$config['layout']['theme']."/icons/priostate".$priority.$status.".png";
		return $url;

	}

	//  --  BUG VIEWS  --  //

	/**
	 *	Contructs HTML Output.
	 *	@access		private
	 *	@return		string
	 */
	private function buildContentAdd()
	{
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$words		= $this->words['bug']['add'];

		//  --  SERVERS & PROJECTS  --  //
		$list		= Logic_Project::getServerProjectList();
		$opt_project = array();
		foreach( $list as $id	=> $serverproject )
			$opt_project[$id] = $serverproject;
		$opt_project['_selected'] = $request->get( 'add_project' );

		//  --  PRIORITIES  --  //
		$opt_priority = $this->words['main']['priorities'];
		$opt_priority['_selected'] = $request->get( 'add_priority' );

		//  --  TYPES  --  //
		$opt_type	= array();
		foreach( $this->words['main']['types'] as $key => $word )
			$opt_type[$key] = $word;
		$opt_type['_selected']	= $request->get( 'add_type' );

		//  --  SOURCES  --  //
		$sources	= array(
			"opt_project"	=> $opt_project,	
			"opt_type"		=> $opt_type,	
			"opt_priority"	=> $opt_priority,	
			);

		$ui	= $this->buildForm( 'bug', 'addBug', 'bug', 'add', array(), $sources );
		$button_cancel	= $this->html->LinkButton( "./", $words['button_cancel'], 'but cancel' );
		$button_add		= $this->html->Button( 'addBug', $words['button_add'], 'but add' );
		$ui['field_button_cancel']	= $this->html->Field( '', $button_cancel );
		$ui['field_button_add']		= $this->html->Field( '', $button_add );

		$ui['form']		= $this->html->Form( 'addBug', "?link=".$request->get( 'link' ) );
		$ui['colgroup']	= $this->html->ColumnGroup( "30%", "70%" );
		$ui['caption']	= $this->html->TableCaption( $words['caption'] );
		$ui['heading']	= $words['heading'];

		return $this->loadTemplate( 'bug.add', $ui );
	}

	//  --  PRIVATE FUNCTIONS  --  //
	/**
	 *	Insert a Link to a BugEntry if pattern '@bug ?#([0-9]+)@i' is found.
	 *	@access		private
	 *	@param		string	Text to substitute in
	 *	@return		string
	 */
	private function includeBugLink( $string )
	{
		if( preg_match( "@bug ?#[0-9]@i", $string ) )
			$string	= preg_replace_callback( "@bug ?#([0-9]+)@i", array( $this, 'includeBugLink_callback' ), $string );
		return $string;
	}

	/**
	 *	Callback for BugViews::includeBugLink.
	 *	@access		private
	 *	@param		array	matches		Resulting array of matches
	 *	@return		string
	 */
	private function includeBugLink_callback( $matches )
	{
		$request	= $this->registry->get( 'request' );

		$link		= $this->html->Link( "?link=".$request->get( 'link' )."&bug_id=".$matches[1], "Bug #".$matches[1] );
		return $link;
	}
}
?>
